package com.whksoft.mymapplugin.Listener;

import com.whksoft.mymapplugin.API.Event.PaymentCanceledEvent;
import com.whksoft.mymapplugin.utils.CancelReason;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;

import static com.whksoft.mymapplugin.utils.flagUtils.haveQRFlag;

/**
 * 检测玩家右键展示框
 */
public class clickFrame implements Listener{

    Plugin plugin;

    public clickFrame(Plugin plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void itemFrame(PlayerInteractAtEntityEvent e) {
        PaymentCanceledEvent event = new PaymentCanceledEvent(e.getPlayer(), CancelReason.RIGHT_CLICK_TO_FRAME);
        if (e.getRightClicked().getType() == EntityType.ITEM_FRAME) {
            NamespacedKey key = new NamespacedKey(plugin, "isQRCode");
            ItemStack clicked = e.getPlayer().getInventory().getItemInMainHand();
            ItemMeta itemMeta = clicked.getItemMeta();
            if (itemMeta == null) return;
            if (haveQRFlag(key, clicked)) {
                e.getPlayer().getInventory().getItemInMainHand().setAmount(0);
                Bukkit.getPluginManager().callEvent(event);
            }
        }
    }

}
